package action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import proxy.ProxyFactory;
import service.CompanyService;
import service.CompanyServiceInterface;

import beans.Company;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import dao.CompanyDao;
import dao.ExpressDao;

@SuppressWarnings("serial")
public class CompanyAction extends ActionSupport implements ModelDriven<Company>{

	CompanyDao companyDao = new CompanyDao();
	ExpressDao expressDao = new ExpressDao();

	private int companyID;
	private int rows;// easyui页大小
    private int page;// easyui当前页
    private String ids;//删除id集合
    private String returnType;// 返回类型
    private String pageName;//返回页面
    
    private Map<String, Object> companyMap;
    
	private Company company = new Company();
	
	private List<Company> companies;
	
	CompanyServiceInterface companyService = (CompanyServiceInterface) ProxyFactory.getProxyInstance(new CompanyService());
	
	/**
	 * 
	 * 返回页面
	 * @author Devil Zheng
	 * @created 2015-9-16 上午11:39:05
	 * @return
	 */
	public String returnJSP() {
        return pageName;
    }
	
	/**
	 * 
	 * 获取分页
	 * @author Devil Zheng
	 * @created 2015-9-16 上午11:43:56
	 * @return
	 */
	public String getCompanyPage() {
        if (returnType.equals("json")) {
            companyMap = new HashMap<String, Object>();
            companyMap.put("total", companyService.getCompanyNumber());
            companyMap.put("rows", companyService.getCompanyes(rows, page));
            return "jsonSuccess";
        }else{
            return "companyList";
        }
    }
	
	/**
	 * 编辑公司
	 * 
	 * @return
	 */
	public String editCompany() {
		company.setCompanyId(companyID);
		companyDao.edit(company);
		return "success";
	}

	/**
	 * 删除公司
	 * 
	 * @return
	 */
	public String deleteCompany() {
		if (expressDao.getCompanyExpressBranch(companyID).size()>0) {
		} else {
			company.setCompanyId(companyID);
			companyDao.delete(company);
		}
		return "success";
	}

	/**
	 * 新增公司
	 * 
	 * @return
	 */
	public String registerCompany() {
		companyDao.save(company);
		return "success";
	}

	
	/**
	 * 通过ID获取公司
	 */
	public String getCompanyById() {
		company=companyDao.getCompanyById(companyID);
		return "success";
	}
	
	public List<Company> getCompanies() {
		return companies;
	}

	public void setCompanies(List<Company> companies) {
		this.companies = companies;
	}

	public int getCompanyID() {
		return companyID;
	}

	public void setCompanyID(int companyID) {
		this.companyID = companyID;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public String getIds() {
        return ids;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

    public String getReturnType() {
        return returnType;
    }

    public void setReturnType(String returnType) {
        this.returnType = returnType;
    }

    public String getPageName() {
        return pageName;
    }

    public void setPageName(String pageName) {
        this.pageName = pageName;
    }
    
    public Map<String, Object> getCompanyMap() {
        return companyMap;
    }

    public void setCompanyMap(Map<String, Object> companyMap) {
        this.companyMap = companyMap;
    }
    
    @Override
    public Company getModel() {
        return company;
    }
}
